import React from 'react';
import { Space, Table } from 'antd';
import axios from 'axios'
import  { useEffect, useState } from 'react';

const request = axios.create({
  baseURL: "http://localhost:8080",
});

const pass = (index) => {
  var store = window.localStorage
  var user_name = store.getItem("UserName")
  var review_data = {
    "reviewer": user_name,
    "id": index.id,
    "review": true
  }
  request.post("/api/review", review_data).then (  
    res => {  // 成功的回调函数
      alert("审核成功")
      window.location.reload()
    }
  ).catch(res => {  // 错误处理
      console.log(res.data.res)
      console.log("user_fail")
  })
}

const reject = (index) => {
  var store = window.localStorage
  var user_name = store.getItem("UserName")
  var review_data = {
    "reviewer": user_name,
    "id": index.id,
    "review": false
  }
  request.post("/api/review", review_data).then (  
    res => {  // 成功的回调函数
      alert("审核成功")
      window.location.reload()
    }
  ).catch(res => {  // 错误处理
      console.log(res.data.res)
      console.log("user_fail")
  })
}

var dataSource;

const columns2 = [
    {
      title: '名称',
      dataIndex: 'title',
      key: 'index',
    },
    {
      title: '上传用户',
      dataIndex: 'uploader',
      key: 'index',
    },
    {
        title: '链接',
        key: 'index',
        render: (index) => (
            <Space size="middle">
              <a onClick={() => window.location.href=index.path}>跳转</a>
            </Space>
        ),
    },
    {
        title: '审核',
        key: 'index',
        render: (index) => (
            <Space size="middle">
              <a onClick={() => pass(index)}>通过</a>
              <a onClick={() => reject(index)}>不通过</a>
            </Space>
        ),
    }
];

const AdminTable2 = () => {
  const [TableData, setTableData] = useState("")

  useEffect(()=>{
    const getData = async() => {
      var store = window.localStorage
      var class_name = store.getItem("ClassPage")
      var user_name = store.getItem("UserName")
      const request = axios.create({
        baseURL: "http://localhost:8080"
      });
      var table_data = {
        "course": class_name,
        "user": user_name
      }
      request.post('/api/getUnreviewUrl', table_data).then(  
        res => {  
          dataSource = res.data
          console.log(res.data)
          let tmp = [...dataSource]
          setTableData(tmp)
        }
      ).catch(res => {  // 错误处理
        console.log(res.data.res)
      })
    }
    getData()
  },[])

  return (
    <Table dataSource={TableData} columns={columns2} />
  )
}

export default AdminTable2